package com.popcorn.board.controller;
// 
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.action.Action;
import com.popcorn.action.ActionForward;

@WebServlet(name = "BoardController",urlPatterns = "*.zo")

public class BoardController extends javax.servlet.http.HttpServlet
      implements javax.servlet.Servlet {
   /**
    * 
    */
   private static final long serialVersionUID = 1L;

   protected void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html; charset=UTF-8");
      response.setCharacterEncoding("UTF-8");
      request.setCharacterEncoding("UTF-8");
      
      String RequestURI = request.getRequestURI(); 
      String contextPath = request.getContextPath();   
      String command = RequestURI.substring(contextPath.length());
      
      System.out.println("========================BoardController======================== ");
      System.out.println("RequestURI : "+ RequestURI);
      System.out.println("contextPath : "+ contextPath);
      System.out.println("command : "+ command);
      
      ActionForward forward = null;
      Action action = null;

      if (command.equals("/board/BoardInsertDB.zo")) { // �깅줉
         System.out.println("/Board/BoardInsertDB.zo");
         forward = new ActionForward();
         action = new BoardAddAction();
         forward.setRedirect(false);
         forward.setPath("/board/BoardInsert.jsp");
      }      
      else if (command.equals("/board/BoardListDB.zo")) { // 由ъ뒪��
          System.out.println("/board/BoardListDB.zo");
          forward = new ActionForward();
          action = new BoardListAction();
          forward.setRedirect(false);
          forward.setPath("/BoardView.jsp");
      }
     
          else if (command.equals("/board/BoardDeleteDB.zo")) { // 由ъ뒪��
              System.out.println("/board/BoardDeleteDB.zo");
              forward = new ActionForward();
              action = new BoardDeleteAction();
              forward.setRedirect(false);
              forward.setPath("/board/BoardView.jsp");          
      }
      
          else if (command.equals("/board/BoardModifyDB.zo")) { // 由ъ뒪��
              System.out.println("/board/BoardModifyDB.zo");
              forward = new ActionForward();
              action = new BoardModifyAction();
              forward.setRedirect(false);
              forward.setPath("/board/BoardView.jsp");          
      }
      
      else if (command.equals("/RMSUpdateViewDB.zo")) { // �섏젙
          System.out.println("/RMSUpdateViewDB.zo");
          forward = new ActionForward();
          //action = new ReservationUpdateViewAction();
          forward.setRedirect(false);
          forward.setPath("/RMSUpdate.jsp");
       }
       else if (command.equals("/board/BoardDetailDB.zo")) { // �곸꽭蹂닿린
          System.out.println("/board/BoardDetailDB.zo");
          forward = new ActionForward();
          action = new BoardDetailAction();
          forward.setRedirect(false);
          forward.setPath("/BoardDetail.jsp");
       }
       else if (command.equals("/board/BoardUpdateDB.zo")) { // �곸꽭蹂닿린
           System.out.println("/BoardUpdateDB.zo");
           forward = new ActionForward();
           action = new BoardUpdateAction();
           forward.setRedirect(false);
           forward.setPath("/board/BoardView.jsp");
        }      
       else if (command.equals("/RMSDeleteDB.zo")) { // ��젣
           System.out.println("/RMSDeleteDB.zo");
           forward = new ActionForward();
           //action = new ReservationDeleteAction();
           forward.setRedirect(false);
           forward.setPath("/view.jsp");
       }
       else if (command.equals("/RMSSelectDB.zo")) { // 寃�깋
           System.out.println("/RMSSelectDB.zo");
           forward = new ActionForward();
          // action = new ReservationSelectAction();
           forward.setRedirect(false);
           forward.setPath("/view.jsp");
       }
      
		try{
		   forward = action.execute(request, response);
		}catch(Exception e){
		   e.printStackTrace();
		}
       
      if(forward != null){
		   if(forward.isRedirect()){
			   response.sendRedirect(forward.getPath());
		   }else{
			   RequestDispatcher dispatcher=
				   request.getRequestDispatcher(forward.getPath());
			   dispatcher.forward(request, response);
		   }
      }
   }

   protected void doGet(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      doProcess(request, response);
   }

   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      doProcess(request, response);
   }
}